package com.yihuu.controller;

import com.yihuu.common.Result;
import com.yihuu.entity.User;
import com.yihuu.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by yuhui on 2017/8/15.
 */
@Controller
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param uname
     * @param upassword
     * @param session
     * @param servletRequest
     * @return
     */
    @RequestMapping("login")
    public String login(String uname, String upassword, HttpSession session, HttpServletRequest servletRequest){
        Result result = userService.login(uname, upassword);
        if(result.isSuccess()){
            User data = (User) result.getData();
            if(StringUtils.isBlank(data.getAnswer()) && StringUtils.isBlank(data.getQuestion())){
                session.setAttribute("current_user",data);
                return "user/question";
            }else{
                data.setUpassword("");
                data.setQuestion("");
                data.setAnswer("");
                session.setAttribute("current_user",data);
                return "index";
            }
        }else{
            servletRequest.setAttribute("msg",result.getMsg());
            return "login";
        }
    }

    /**
     * 用户注销
     * @param session
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }

    /**
     * 用户注册
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("register")
    public String register(User user,HttpServletRequest request){
        Result result = userService.register(user);
        if(result.isSuccess()){
            request.getSession().setAttribute("current_user",result.getData());
            return "user/question";
        }else{
            return "register";
        }
    }

    @RequestMapping("question/set")
    public String setQuestionAndAnswer(String question,String answer,HttpSession session,HttpServletRequest request){
        User currentUser = (User)session.getAttribute("current_user");
        if(currentUser!=null&& currentUser.getQuestion()==null&&currentUser.getAnswer()==null){
            currentUser.setQuestion(question);
            currentUser.setAnswer(answer);
            Result result = userService.setQuestion(currentUser);
            if(result.isSuccess()){
                return "index";
            }else{
                return "user/question";
            }
        }else{
            request.setAttribute("msg","用户未注册");
            return "register";
        }
    }

    /**
     * 查找问题
     * @param uname
     * @return
     */
    @RequestMapping("question")
    @ResponseBody
    public Result getQuestion(String uname){
        Result question = userService.getQuestion(uname);
        return question;
    }

    /**
     * 验证答案是否正确
     * @return
     */
    @RequestMapping("answer/confirm")
    @ResponseBody
    public Result get(String uname,String question,String answer){
        Result result = userService.lossPassword(uname, question, answer);
        return result;
    }
    /**
     * 重置密码
     * @return
     */
    @RequestMapping("password/reset")
    @ResponseBody
    public Result resetPassword(String uname,String question,String answer,String upassword){
        Result result = userService.lossPassword(uname, question, answer, upassword);
        return result;
    }

    /**
     * 登陆验证用户名是否存在
     * @param type
     * @param uname
     * @param uphone
     * @return
     */
    @RequestMapping("check")
    @ResponseBody
    public Boolean checkColumnInLogin(String type,String uname,String uphone){
        Boolean exits = userService.checkColumn(type, uname,uphone);
        return exits;
    }

    /**
     * 注册验证用户名，或者电话是否存在
     * @param type
     * @param uname
     * @param uphone
     * @return
     */
    @RequestMapping("check/register")
    @ResponseBody
    public Boolean checkColumn2InRegister(String type,String uname,String uphone){
        Boolean exits = userService.checkColumn(type, uname,uphone);
        return !exits;
    }

    /**
     * 更新用户信息
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("save")
    public String updateInfo(User user,HttpSession session,HttpServletRequest request){
        User currentUser = (User)session.getAttribute("current_user");
        if(currentUser!=null){
            user.setUid(currentUser.getUid());
            Result result = userService.updateInfo(user);
            User newUser = (User) result.getData();
            newUser.setAnswer("");
            newUser.setUpassword("");
            session.setAttribute("current_user",newUser);
            return "index";
        }else{
           request.setAttribute("msg","用户未登录");
           return "login";
        }
    }

}
